import React from 'react'
import {Form,Input,Select} from 'antd'
import PropTypes from 'prop-types'
const FormItem = Form.Item
const SelectOption = Select.Option
const itemLayout = {
  labelCol: {
    span:8
  },
  wrapperCol: {
    span:14
  },
}

class UserForm extends React.Component{
  constructor(props){
    super(props)
    this.state = {
      user:props.user
    }
  }
  componentWillMount(){
    this.props.setForm(this.props.form)
  }
  render(){
    const {form} = this.props
    const {user} = this.state
    const {getFieldDecorator} = form
    return (
      <Form>
        <FormItem label="登录用户账号：" key="username" hasFeedback {...itemLayout}>
          {getFieldDecorator('user.username', {
            initialValue:user.username,
            rules: [{ required: true, message: '请输入用户名' }],
          })(
            <Input/>
          )}
        </FormItem>
        <FormItem label="用户登录密码：" key="password" hasFeedback {...itemLayout}>
          {getFieldDecorator('user.password', {
            initialValue:user.password,
            rules: [{ required: true, message: '请输入登录密码' }],
          })(
            <Input/>
          )}
        </FormItem>
        <FormItem label="用户状态：" key="enabled" {...itemLayout}>
          {getFieldDecorator('user.enabled', {
            initialValue:user.enabled,
            rules: [{ required: true, message: '填写用户状态' }],
          })(
            <Select>
              <SelectOption value="1">可用</SelectOption>
              <SelectOption value="0">禁用</SelectOption>
            </Select>
          )}
        </FormItem>
        <FormItem label="用户最后登录ip地址：" key="ip" hasFeedback{...itemLayout}>
          {getFieldDecorator('user.ip', {
            initialValue:user.ip,
            rules: [{ required: true, message: '填写用户最后登录ip地址' }],
          })(
            <Input />
          )}
        </FormItem>
      </Form>
    )
  }
}

UserForm.propTypes = {
  form:PropTypes.object.isRequired,
  user:PropTypes.object.isRequired,
  setForm:PropTypes.func,
}

UserForm.defaultProps = {
  setForm:()=>{}
}

export default Form.create()(UserForm)